package com.jinyuan.options;

import com.jinyuan.R;
import com.jinyuan.view.circlemenu.CircleAnimations;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

/**
 * 中部页面
 * 
 * @author azuryleaves
 * 
 */
public class MainActivity extends FragmentActivity implements
		LeftMenuActivity.Callbacks, MainBottomFragment.Callbacks {

	public static String Position = "position", ViewId = "viewId";
	public static ImageView center_left_ImageView, main_circle_bg_ImageView;
	private FrameLayout center_header_right_FrameLayout;
	private ViewGroup circleViewGroup;
	private View circleIcon;
	private boolean isButtonsShowing;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		center_left_ImageView = (ImageView) findViewById(R.id.main_header_left_ImageView);
		center_left_ImageView.setOnClickListener(clickListener);
		center_header_right_FrameLayout = (FrameLayout) findViewById(R.id.main_header_right_FrameLayout);
		main_circle_bg_ImageView = (ImageView) findViewById(R.id.main_circle_bg_ImageView);
		circleViewGroup = (ViewGroup) findViewById(R.id.composer_buttons_wrapper);
		for (int i = 0; i < circleViewGroup.getChildCount(); i++) {
			circleViewGroup.getChildAt(i).setOnClickListener(clickListener);
		}
		CircleAnimations.initOffset(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		LeftMenuActivity.callbacks = this;
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			int currentScreen = SlidingMenuControlActivity.getCurrentScreen();
			switch (view.getId()) {
			case R.id.main_header_left_ImageView:
				if (currentScreen == 0) {
					SlidingMenuControlActivity.hideMenu();
				} else if (currentScreen == 1) {
					SlidingMenuControlActivity.showMenu();
				}
				break;
			case R.id.main_circle_item1_Button:
				circleItem(view);
				toFragment(((ViewGroup) view.getParent()).getId(), 0);
				break;
			case R.id.main_circle_item2_Button:
				circleItem(view);
				toFragment(((ViewGroup) view.getParent()).getId(), 1);
				break;
			case R.id.main_circle_item3_Button:
				circleItem(view);
				toFragment(((ViewGroup) view.getParent()).getId(), 2);
				break;
			case R.id.main_circle_item4_Button:
				circleItem(view);
				toFragment(((ViewGroup) view.getParent()).getId(), 3);
				break;
			default:
				break;
			}
		}
	};

	private void circleItem(View circleItem) {
		MainBottomFragment.main_tab_RadioGroup.clearCheck();
		circleIcon.startAnimation(CircleAnimations.getRotateAnimation(180, 0,
				300));
		isButtonsShowing = !isButtonsShowing;
		circleItem.startAnimation(CircleAnimations.getMaxAnimation(400));
		for (int j = 0; j < circleViewGroup.getChildCount(); j++) {
			final Button smallIcon = (Button) circleViewGroup.getChildAt(j);
			if (circleItem.getId() != smallIcon.getId()) {
				smallIcon
						.startAnimation(CircleAnimations.getMiniAnimation(300));
				smallIcon.setClickable(false);
				smallIcon.setFocusable(false);
			}
		}
		circleItem.setClickable(false);
		circleItem.setFocusable(false);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				CircleAnimations.startBackgroudAnimationsIn(
						main_circle_bg_ImageView, 300);
			}
		}, 300);
	}

	@Override
	public void onCommonMenuClick(AdapterView<?> adapterView, View view, int i,
			Object data) {
		MainBottomFragment.main_tab_RadioGroup.clearCheck();
		if (center_header_right_FrameLayout != null) {
			center_header_right_FrameLayout.removeAllViews();
		}
		toFragment(adapterView.getId(), i);
	}
	
	@Override
	public void onOperationMenuItemClick(AdapterView<?> adapterView, View view,
			int i, Object data) {
		MainBottomFragment.main_tab_RadioGroup.clearCheck();
		toFragment(adapterView.getId(), i);
	}
	
	private void toFragment(int viewId, int position) {
		OptionsFragment.popBackStack(this);
		Bundle arguments = new Bundle();
		arguments.putInt(Position, position);
		arguments.putInt(ViewId, viewId);
		OptionsFragment optionsFragment = new OptionsFragment();
		optionsFragment.setArguments(arguments);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.main_FrameLayout, optionsFragment).commit();
	}

	public boolean onBackKeyDown(int backStackCount, KeyEvent event) {
		return getSupportFragmentManager().popBackStackImmediate();
	}

	@Override
	public void onCircleViewClick(View view) {
		circleIcon = view
				.findViewById(R.id.composer_buttons_show_hide_button_icon);
		if (!isButtonsShowing) {
			CircleAnimations.startBackgroudAnimationsOut(
					main_circle_bg_ImageView, 300);
			// 中心的动画
			circleIcon.startAnimation(CircleAnimations.getRotateAnimation(0,
					180, 300));
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					// 图标的动画
					CircleAnimations.startAnimationsOut(circleViewGroup, 300);
				}
			}, 300);
		} else {
			// 图标的动画
			CircleAnimations.startAnimationsIn(circleViewGroup, 300);
			// 中心的动画
			circleIcon.startAnimation(CircleAnimations.getRotateAnimation(180,
					0, 300));
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					CircleAnimations.startBackgroudAnimationsIn(
							main_circle_bg_ImageView, 300);
				}
			}, 300);
		}
		isButtonsShowing = !isButtonsShowing;
	}

	@Override
	public void onRadioButtonClick(View view) {
		toFragment(((RadioGroup) view.getParent()).getId(), view.getId());
	}

}
